package top.haichi.webpaper.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.haichi.webpaper.entity.Result;
import top.haichi.webpaper.entity.Student;
import top.haichi.webpaper.mapper.StudentMapper;
import top.haichi.webpaper.service.StudentService;
import top.haichi.webpaper.utils.FileUtils;
import top.haichi.webpaper.utils.JwtUtils;
import top.haichi.webpaper.utils.PatternsUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;


@Service
@Slf4j
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 修改学生信息
     * @param student
     * @return
     */
    public Result setInformation(Student student) {

        log.info(student+"设置了学生信息");
        studentMapper.insert(student);
        return null;
    }

//    /**
//     * 新增学生信息
//     * @param student
//     * @return
//     */
//    public Result addStudent(Student student) {
//        log.info(student+"创建了学生信息");
//
//        if(student.getId()==null||student.getId().length()<1||student.getId().length()>16){
//            return Result.error("学号必须为1~16位");
//        }
//        if(student.getName()==null||student.getName().length()<1||student.getName().length()>10){
//            return Result.error("姓名必须为1~10位");
//        }
//        if(student.getClassName()==null||student.getClassName().length()<1||student.getClassName().length()>10){
//            return Result.error("班级必须为1~10位");
//        }
//        if(!(student.getGender().equals("男")||student.getGender().equals("女")||student.getGender().equals("未知"))){
//            return Result.error("性别输入不正确");
//        }
//        if(student.getMajor()==null||student.getMajor().length()<1||student.getMajor().length()>32){
//            return Result.error("专业必须为1~32位");
//        }
//        if((student.getEmail().length()<1||student.getEmail().length()>128)){
//            return Result.error("邮箱必须为1~128位");
//        }
//        if(!(PatternsUtils.match(student.getEmail(),PatternsUtils.EMAIL_PATTERN))){
//            return Result.error("不符合邮箱格式");
//        }
//        if(studentMapper.getStudentByid(student)!=null){
//            return Result.error("您已经创建过个人信息了，请勿重复创建");
//        }
//        studentMapper.addStudent(student);
//
//        return Result.success("信息录入成功");
//    }

    /**
     * 查询学生信息
     * @param student
     * @return
     */
    public Result getStudent(Student student){
        Student resultStudent = studentMapper.selectById(student.getId());
        return Result.success("查询成功",resultStudent);
    }

//    /**
//     * 获取学生头像
//     * @param student
//     * @param response
//     * @return
//     */
//    @Override
//    public byte[] getPortrait(Student student,HttpServletResponse response) {///files/portraits/
//        try {
//            File file = new File(FileUtils.getJarPath() + "\\resources\\" + student.getId() + "\\images\\portrait" + ".jpg");
//            FileInputStream fileInputStream = new FileInputStream(file);
//            byte[] data = IOUtils.toByteArray(fileInputStream);
//            fileInputStream.close();
//            return data;
//        }catch (Exception e){
//            e.printStackTrace();
//            return null;
//        }
//    }

    /**
     * 更改学生信息
     * @param student
     * @return
     */
    @Override
    public Result updateStudent(Student student) {
        student.setAvatar(null);
            Student tempStudent = studentMapper.selectById(student.getId());
            tempStudent.setName(student.getName());
            tempStudent.setEmail(student.getEmail());
            tempStudent.setBio(student.getBio());
            studentMapper.updateById(tempStudent);
            return Result.success("修改成功!");
    }
}
